'use strict';

import Base from './base.js';

export default class extends Base {

	async jigouAction(){
		// 机构权限类型
		let cate = [3,4];
		let list = await this.model('or_rulemodel').where({category: ['IN', cate]}).select();
		this.assign('list',list)
		return this.display();
	}

	async fendianAction(){
		let cate = [5,6];
		let list = await this.model('or_rulemodel').where({category: ['IN', cate]}).select();
		this.assign('list', list);
		return this.display();
	}

	async appAction(){
		let cate = [1,2];
		let list = await this.model('or_rulemodel').where({category: ['IN', cate]}).select();
		this.assign('list', list);
		return this.display();
	}

	async addAction(){
		if (this.isPost) {
			let data = this.post();
			if (data.id) {
				let chongfu = await this.model('or_rulemodel').where({name: data.name, id: ['!=', data.id]}).find();
				if (!think.isEmpty(chongfu)) {
					return this.fail('板块名称重复');
				}
				let genggai = await this.model('or_rulemodel').where({id: data.id}).update({name: data.name});
				return genggai?this.success('更改成功'):this.fail('更改失败');
			} else {
				let repeat = await this.model('or_rulemodel').where({name: data.name, category: data.category}).select();
				if (!think.isEmpty(repeat)) {
					return this.fail('添加板块重复');
				}
				let adddata = await this.model('or_rulemodel').add(data);
				return adddata?this.success('添加成功'):this.fail('添加失败');
			}

		} else {
			let pid = this.get('pid');
			let ct = this.get('ct');
			let bankuai = 1;
			if (pid) {
				bankuai = await this.model('or_rulemodel').where({id: pid}).find();
			}
			this.assign({'pid':pid,'ct':ct, 'bk': JSON.stringify(bankuai)});

			return this.display();
		}

	}

	async editorAction(){
		if (this.isPost) {
			let data = this.post();
			if (data.id) {
				let chongfu = await this.model('or_rulemodel').where({name:data.name, tags:data.tags, url:data.url, id:['!=', data.id]}).find();

				if (!think.isEmpty(chongfu)) {
					return this.fail('权限重复');
				}
				let gengxin = await this.model('or_rulemodel').update(data);
				return gengxin?this.success('更改成功'):this.fail('更改失败');
			} else {
				let repeat = await this.model('or_rulemodel').where({name: data.name, tags: data.tags, url: data.url}).select();
				if (!think.isEmpty(repeat)) {
					return this.fail('添加权限重复');
				}

				let adddata = await this.model('or_rulemodel').add(data);
				return adddata?this.success('添加成功'):this.fail('添加失败');
			}
			

		} else {
			let id = this.get('id');
			let tj = this.get('tj');
			let ct = this.get('ct');
			let one = await this.model('or_rulemodel').where({id: id}).find();
			one = JSON.stringify(one);

			this.assign({'one': one, 'tj': tj, 'ct':ct});
			return this.display();
		}
		
	}

	async delAction(){
		let id = this.post('id');
		let lb = await this.model('or_rulemodel').where({id: id}).find();
		if (lb.pid) {
			let res = await this.model('or_rulemodel').where({id: id}).delete();
			return res?this.success('删除成功'):this.fail('删除失败');
		} else {
			let del = await this.model('or_rulemodel').where('id="'+id+'" OR pid="'+id+'"').delete();
			return del?this.success('删除成功'):this.fail('删除失败');
		}
	}
}